/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.dao.impl;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.camelot.ccr.global.dao.genericimpl.GenericHibernateDaoImpl;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.controller.TeamController;
import com.camelot.ccr.system.dao.MatchTeamDao;
import com.camelot.ccr.system.dao.TeamDao;
import com.camelot.ccr.system.persistence.MatchTeam;
import com.camelot.ccr.system.persistence.MatchType;
import com.camelot.ccr.system.persistence.Team;

/**
 * <pre>
 * 车队
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-20 下午10:30:05
 */
@Repository
public class TeamDaoImpl extends GenericHibernateDaoImpl<Team, String> implements TeamDao {

	@Resource
	private MatchTeamDao matchTeamDao;
	
	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.TeamDao#saveTeam(com.camelot.ccr.system.persistence.Team, org.springframework.web.multipart.commons.CommonsMultipartFile, org.springframework.web.multipart.commons.CommonsMultipartFile)
	 */
	@Override
	public void saveTeam(Team team, CommonsMultipartFile teamPhotoUp, CommonsMultipartFile teamPhotoDown,String[] matchTypes) throws IOException {
		String teamPhotoUpRealPathDir = team.getTeamPhotoUp();
		String teamPhotoDownRealPathDir = team.getTeamPhotoDown();
		String teamPhotoUpPath = Utility.uploadFile(teamPhotoUp, teamPhotoUpRealPathDir,TeamController.teamHeadUrlPath);
		String teamPhotoDownPath = Utility.uploadFile(teamPhotoDown, teamPhotoDownRealPathDir,TeamController.teamHeadUrlPath);
		team.setTeamPhotoUp(teamPhotoUpPath);
		team.setTeamPhotoDown(teamPhotoDownPath);
		
		save(team);
		if(Utility.isNotEmpty(matchTypes)){
			for (int i = 0; i < matchTypes.length; i++) {
				MatchType mtype = new MatchType();
				MatchTeam mTeam = new MatchTeam();
				mtype.setId(matchTypes[i]);
				mTeam.setMatch(mtype);
				mTeam.setTeam(team);
				
				matchTeamDao.save(mTeam);
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.TeamDao#randGetList()
	 */
	@Override
	public List<Team> randGetList() {
		String hql = "from Team t order by t.id " ;
		return queryListPage(hql, 0, 5);
	}
	
	


	
}
